

import os
import sys
import subprocess

import argparse

import Notify
from ArcAlvus import ArcAlvus
from Config import Config


parser = argparse.ArgumentParser(prog='arcalvus', description='CLI password manager.', epilog="v1.0")

group1 = parser.add_argument_group()
group2 = parser.add_argument_group()
group3 = parser.add_argument_group()
group4 = parser.add_argument_group()
group5 = parser.add_argument_group()

group1.add_argument("-p", "--get-password", action='store_true')
group1.add_argument("-l", "--get-login", action='store_true')
group1.add_argument("-o", "--get-otp", action='store_true')
group1.add_argument("-g", "--get", type=str, metavar=('<key>'))

group2.add_argument("-S", "--set-storage", type=str, metavar=('<storage_name>'))

group3.add_argument("-C", "--create-storage", action='store_true')
group3.add_argument("-E", "--edit", action='store_true')

# group4.add_argument("--config", metavar=('<config>'))

group5.add_argument("--gen-password", metavar=('"20 a-zA-Z0-9 _-#@"'), help="At the beginning the length, then chars and special chars.")
group5.add_argument("--open", action='store_true')
group5.add_argument("--show", action='store_true')
group5.add_argument("--init", action='store_true')
group5.add_argument("--change-gpg-key", metavar=('<new_gpg_id>'))

args = parser.parse_args()


try:
    
    # config = Config(path_config_file=args.config)
    config = Config()
    
except KeyboardInterrupt:
    sys.exit()
    
except Exception as error:
    print( f"ConfigError: {error}" )


def Main():
    if args.init:
        config.CREATE_CONFIG()
        sys.exit()
    
    elif args.get_password:
        print( arcalvus.GET(key="PASSWORD", storage=args.set_storage, show=args.show) )
        sys.exit()
        
    elif args.get_login:
        print( arcalvus.GET(key="LOGIN", storage=args.set_storage, show=args.show) )
        sys.exit()
        
    elif args.get_otp:
        otp_secret_key = arcalvus.GET(key="OTP", storage=args.set_storage, show=True)
        otp_key = arcalvus.GET_OTP( otp_secret_key=otp_secret_key )
        
        print( otp_key )
        
        Notify.Send(message=f"OTP: {otp_key}")
        
        sys.exit()
        
    elif args.get:
        print( arcalvus.GET(key=str(args.get), storage=args.set_storage, show=args.show) )
        sys.exit()
        
    elif args.create_storage:
        
        if not args.set_storage:
            args.set_storage = input("Enter StorageName: ").strip()
        else:
            args.set_storage = args.set_storage
            
        arcalvus.CREATE( args.set_storage )
        
    elif args.open:
        arcalvus.STORAGE_EDIT(storage=args.set_storage, read_only=True)
        
    elif args.edit:
        arcalvus.STORAGE_EDIT(storage=args.set_storage, read_only=False)
    
    elif args.gen_password:
        print( arcalvus.RANDOM( str(args.gen_password) ) )
        
    elif args.change_gpg_key:
        arcalvus.CHANGE_GPG_KEY( args.change_gpg_key )
        sys.exit()
    
    else:
        parser.print_help()


if __name__ == "__main__":
    
    try:
        
        arcalvus = ArcAlvus( config.GET_STORAGE_PATH() )
        Main()
    
    except subprocess.CalledProcessError: ...
    except KeyboardInterrupt: ...
    except EOFError: ...
    
    except KeyError as error:
        print(f"Error, no key {error} in storage {arcalvus.storage_name}.")
    
    except Exception as error:
        print(f"Error: {error}")
    
    finally:
        sys.exit()
